package com.heny.demo.strategy.file;

import com.heny.demo.constants.Const;
import com.heny.demo.entity.TurnPoint;
import com.heny.demo.util.GeometryUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Csv文件读取实现类
 *
 * @author 邢益萌
 * {@code @date} 2025/9/12 14:16
 */
public class CsvFileImportStrategy implements FileImportStrategy {

    @Override
    public List<TurnPoint> importFile(InputStream inputStream) {
        //存放转换后的坐标
        List<TurnPoint> points = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while ((line = br.readLine()) != null) {
                //格式: 坐标index, x坐标, y坐标, z坐标
                String[] arr = line.split(Const.COMMA);
                if (arr.length < 3) {
                    continue;
                }
                //获取解析后的坐标对象
                TurnPoint turnPoint = GeometryUtil.getTurnPoint(arr);
                points.add(turnPoint);
            }
        } catch (IOException e) {
            throw new RuntimeException("CSV文件解析失败", e);
        }
        return points;
    }
}
